﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

namespace MyCadDll.MyObject
{
    public class MyText : MyCadObject, IComparable
    {
        string m_text;
        int m_iRow;

        public int IRow
        {
            get { return m_iRow; }
            set { m_iRow = value; }
        }
        int m_iCol;

        public int ICol
        {
            get { return m_iCol; }
            set { m_iCol = value; }
        }

        public string Text
        {
            get { return m_text; }
            set { m_text = value; }
        }
        MyPoint m_point;

        public MyPoint Point
        {
            get { return m_point; }
            set { m_point = value; }
        }
        MyLine m_leftLine;

        public MyLine LeftLine
        {
            get { return m_leftLine; }
            set { m_leftLine = value; }
        }
        MyLine m_rightLine;

        public MyLine RightLine
        {
            get { return m_rightLine; }
            set { m_rightLine = value; }
        }
        MyLine m_topLine;

        public MyLine TopLine
        {
            get { return m_topLine; }
            set { m_topLine = value; }
        }
        MyLine m_bottomLine;
        public MyLine BottomLine
        {
            get { return m_bottomLine; }
            set { m_bottomLine = value; }
        }
        public MyText(string text, MyPoint p1)
        {
            m_text = text;
            m_point = p1;
        }

        /// <summary>
        /// 根据Chart中的直线数组确定该文字左直线和所处的列
        /// </summary>
        /// <param name="chart"></param>
        //public void SetLeftLine(Chart chart)
        //{
        //    foreach (MyLine line in chart.ColLineList)
        //    {

        //        SetLeftLine(line);
        //    }
        //    if (LeftLine == null)
        //    {
        //        LeftLine = new MyLine(this.Point, new MyPoint(Point.X + m_text.Length, Point.Y));
        //    }
        //    if (chart.ColIndexDic.ContainsKey((int)LeftLine.P1.X))
        //        ICol = chart.ColIndexDic[(int)LeftLine.P1.X];
        //    else
        //        ICol = -1;
        //}

        //private void SetLeftLine(MyLine line)
        //{
        //    double dx = Point.X - line.P1.X;
        //    if (LeftLine == null)
        //    {
        //        if (line.PointInSect(Point) && line.P1.X <= Point.X)
        //            LeftLine = line;
        //    }
        //    else
        //    {
        //        double dx1 = Point.X - LeftLine.P1.X;
        //        if (dx < dx1 && line.PointInSect(Point) && dx >= 0)
        //        {
        //            LeftLine = line;
        //        }
        //    }
        //}

        //public void SetTopLine(Chart chart)
        //{
        //    foreach (MyLine line in chart.RowLineList)
        //    {
        //        SetTopLine(line);
        //    }
        //    if (TopLine == null)
        //    {
        //        TopLine = new MyLine(this.Point, new MyPoint(Point.X, Point.Y + 10));
        //    }
        //    if (chart.RowIndexDic.ContainsKey((int)TopLine.P1.Y))
        //        IRow = chart.RowIndexDic[(int)TopLine.P1.Y];
        //    else
        //        IRow = -1;
        //}

        //private void SetTopLine(MyLine line)
        //{
        //    double dy = line.P1.Y - Point.Y;
        //    if (TopLine == null)
        //    {
        //        if (line.PointInSect(Point) && line.P1.Y >= Point.Y)
        //            TopLine = line;
        //    }
        //    else
        //    {
        //        double dy1 = TopLine.P1.Y - Point.Y;
        //        if (dy < dy1 && line.PointInSect(Point) && dy >= 0)
        //        {
        //            TopLine = line;
        //        }
        //    }
        //}
        public void SetRowColIndex(Chart chart)
        {
            int n1 = (int)(LeftLine.P1.X);
            if (this.LeftLine != null && chart.ColIndexDic.ContainsKey(n1))
                ICol = chart.ColIndexDic[n1];
            else
                ICol = 0;
            n1=(int)(BottomLine.P1.Y);
            if (this.BottomLine != null && chart.RowIndexDic.ContainsKey(n1))
                IRow = chart.RowIndexDic[n1];
            else
                IRow = 0;

        }

        public void SetLine(Chart chart)
        {
            SetLeftLine(chart);
            SetRightLine(chart);
            SetTopLine(chart);
            SetBottomLine(chart);
        }

        private void SetLeftLine(Chart chart)
        {
            int c1 = chart.ColLineList.IndexOf(this);
            int i = c1 - 1;
            MyCadObject obj;
            MyLine line;
            while (i >= 0)
            {
                obj = chart.ColLineList[i];
                if (obj is MyLine)
                {
                    line = obj as MyLine;
                    if (line.PointInSect(this.Point))
                    {
                        this.LeftLine = line;
                        break;
                    }
                }
                i--;
            }
            if (LeftLine == null)
            {
                MyLine newLine = new MyLine(this.Point.inflate(0, 0), this.Point.inflate(0, 10));
                LeftLine = newLine;
                chart.ColLineList.Add(newLine);
            }
            //if (LeftLine != null && chart.ColIndexDic.ContainsKey((int)LeftLine.P1.X))
            //    ICol = chart.ColIndexDic[(int)LeftLine.P1.X];
            //else
            //    ICol = 0;
        }

        private void SetRightLine(Chart chart)
        {
            int c1 = chart.ColLineList.IndexOf(this);
            int i = c1 + 1;
            int n = chart.ColLineList.Count;
            MyCadObject obj;
            MyLine line;

            while (i <n)
            {
                obj = chart.ColLineList[i];
                if (obj is MyLine)
                {
                    line = obj as MyLine;
                    if (line.PointInSect(this.Point))
                    {
                        this.RightLine = line;
                        break;
                    }
                }
                i++;
            }
        }

        private void SetTopLine(Chart chart)
        {
            int r1 = chart.RowLineList.IndexOf(this);
            int i = r1 - 1;
            int n=chart.RowLineList.Count;
            MyCadObject obj;
            MyLine line;
            while (i >=0)
            {
                obj = chart.RowLineList[i];
                if (obj is MyLine)
                {
                    line = obj as MyLine;
                    if (line.PointInSect(this.Point))
                    {
                        this.TopLine = line;
                        break;
                    }
                }
                i--;
            }
          
        }

        private void SetBottomLine(Chart chart)
        {
            int r1 = chart.RowLineList.IndexOf(this);
            int i = r1 + 1;
            int n = chart.RowLineList.Count;
            MyCadObject obj;
            MyLine line;

            while (i <n)
            {
                obj = chart.RowLineList[i];
                if (obj is MyLine)
                {
                    line = obj as MyLine;
                    if (line.PointInSect(this.Point))
                    {
                        this.BottomLine = line;
                        break;
                    }
                }
                i++;
            }
            if (BottomLine == null)
            {
                MyLine newLine = new MyLine(this.Point.inflate(0, -2), this.Point.inflate(10, -2));
                this.BottomLine = newLine;
                chart.RowLineList.Insert(0, newLine); //Add(newLine);
            }
            //if (BottomLine != null && chart.RowIndexDic.ContainsKey((int)BottomLine.P1.Y))
            //    IRow = chart.RowIndexDic[(int)BottomLine.P1.Y];
            //else
            //    IRow = 0;
        }

        public int CompareTo(object obj)
        {
            if (obj is MyText)
            {
                MyText text = obj as MyText;
                int ibottom, ileft;
                if (BottomLine != null && text.BottomLine != null)
                    ibottom = this.BottomLine.CompareTo(text.BottomLine);
                else
                    ibottom = 0;
                if (LeftLine != null && text.LeftLine != null)
                    ileft = this.LeftLine.CompareTo_X(text.LeftLine);
                else
                    ileft = 0; 
                if (ibottom == 0)
                {
                    return ileft;
                }
                else
                {
                    return ibottom;
                }
            }
            throw new NotImplementedException();
        }

        public override  int CompareTo_X(object obj)
        {
            if (obj is MyText)
            {
                MyText text =obj as MyText;
                return this.Point.CompareTo_X(text.Point); 
            }
          
            throw new NotImplementedException();
        }

        public override int CompareTo_Y(object obj)
        {
            if (obj is MyText)
            {
                MyText text = obj as MyText;
                return this.Point.CompareTo_Y(text.Point);
            }
           
            throw new NotImplementedException();
        }

        public  int CompareTo_Line_X(object obj)
        {
            if (obj is MyText)
            {
                MyText text = obj as MyText;
                int itop, ileft;
                itop = BottomLine.CompareTo(text.BottomLine);
                ileft = LeftLine.CompareTo_X(text.LeftLine);
                if (ileft== 0)
                {
                    return itop;
                }
                else
                {
                    return ileft;
                }
            }
            throw new NotImplementedException();
        }

        public int CompareTo_Line_Y(object obj)
        {
            return CompareTo(obj);
        }
    }
   
}
